package com.ttb.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class UserDAO {
	private SQLiteDatabase database;
	private DBHandler dbHandler;
	private String[] allColums = {DBHandler.USER_KEY_ID, DBHandler.USER_KEY_PASS};
	private static final String USER_TABLE = DBHandler.USER_TABLE;
	private static final String ID = DBHandler.USER_KEY_ID;
	private static final String PASSWORD =  DBHandler.USER_KEY_PASS;

	
	public UserDAO(Context context)
	{
		dbHandler = new DBHandler(context);
	}
	public void open()
	{
		database = dbHandler.getWritableDatabase();
	}
	public void close()
	{
		dbHandler.close();
	}
	//add blocker
	public void addUser(User user)
	{
		this.open();
		ContentValues values = new ContentValues();
		values.put(PASSWORD, user.getPassword());
		//insert row
		database.insert(USER_TABLE, null, values);
		this.close();
	}
	//get single blocker
	public User  getUser(int id) {
		this.open();
		Cursor cursor = database.query(USER_TABLE, 
				allColums, 
				ID + "=?", new String[] {String.valueOf(id)}, 
				null, null, null);
		if(cursor != null)
		{
			cursor.moveToFirst();
			
		}
		User user = new User();
		user.setId(Integer.parseInt(cursor.getString(0)));
		user.setPassword(cursor.getString(1));
		cursor.close();
		this.close();
		//return user
		return user;
	}
	//Get all users
	public List<User> getAllUsers() {
		List<User> userList = new ArrayList<User>();
	    // Select All Query
	    String selectQuery = "SELECT  * FROM " + USER_TABLE;
	 
	    this.open();
	    Cursor cursor = database.rawQuery(selectQuery, null);
	 
	    // looping through all rows and adding to list
	    if (cursor.moveToFirst()) {
	        do {
	            User user = new User();
	            user.setId(Integer.parseInt(cursor.getString(0)));
	            user.setPassword(cursor.getString(1));
	            userList.add(user);
	        } while (cursor.moveToNext());
	    }
	    cursor.close();
	    this.close();
	    // return user list
	    return userList;
	}
	//delete single user
	public void deleteUser(int id)
	{
		this.open();
		Log.d("DELETE BLOCKER ID",String.valueOf(id) );
		database.delete(USER_TABLE, ID + "=?" , new String[]{String.valueOf(id)});
		this.close();
	}
	//update single user
	public void updateUser(User user)
	{
		this.open();
		ContentValues values = new ContentValues();
		values.put(PASSWORD, user.getPassword());
		//update
		database.update(USER_TABLE, values,ID + "=?",
				new String[]{String.valueOf(user.getId())});
		this.close();
	}
	// Getting users Count
    public int getUserCount() {
    	
        String countQuery = "SELECT  * FROM " + USER_TABLE;
        this.open();
        Cursor cursor = database.rawQuery(countQuery, null);
        int count = cursor.getCount();
        cursor.close();
        this.close();
        // return count
        return count;
    }
    
}
